//
//  TTFirstViewController.m
//  TTBaseProject
//
//  Created by shny588 on 16/7/8.
//  Copyright © 2016年 tt. All rights reserved.
//

#import "TTFirstViewController.h"

@interface TTFirstViewController ()

@property (nonatomic, strong) TTVerificationCodeView        *codeView;

@end

@implementation TTFirstViewController

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    int r = arc4random() % 250;
    int g = arc4random() % 250;
    int b = arc4random() % 250;
    self.view.backgroundColor = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0];
    
    //测试区域
    NSString *verifyString = @"412921197610081663";
    BOOL isUrl = [TTVerifiTool validUrl:verifyString];
    BOOL isNum = [TTVerifiTool validNumber:verifyString];
    BOOL isMail = [TTVerifiTool validEmail:verifyString];
    BOOL isPhone = [TTVerifiTool validPhone:verifyString];
    BOOL isCh = [TTVerifiTool validChinese:verifyString];
    BOOL isNumb = [TTVerifiTool validNumberForRegular:verifyString];
    BOOL isLetter = [TTVerifiTool validLetterForRegular:verifyString];
    BOOL isIDCard = [TTVerifiTool verifyIDCardNumber:verifyString];
    
    NSLog(@"isurl:%d___isNum:%d___isMail:%d___isPhone:%d___isCh:%d___isNumb:%d___isLetter:%d___isIDCard:%d", isUrl, isNum, isMail, isPhone, isCh, isNumb, isLetter, isIDCard);
    
    UIButton *button = [UIButton buttonWithType:0];
    button.frame = CGRectMake(100, 50, 100, 30);
    [button setTitle:@"再来一次" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    _codeView = [[TTVerificationCodeView alloc] initWithFrame:CGRectMake(100, 200, 200, 100)];
    _codeView.getCode = ^(NSString *codeString){
        NSLog(@"\n-=-=-=-=-=-=-=验证码：%@", codeString);
    };
    [self.view addSubview:_codeView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UI
#pragma mark - CustomDelegate
#pragma mark - event response
#pragma mark - private methods
- (void)buttonAction:(UIButton *)button {
    [_codeView removeFromSuperview];
    
    _codeView = [[TTVerificationCodeView alloc] initWithFrame:CGRectMake(100, 200, 200, 100)];
    _codeView.getCode = ^(NSString *codeString){
        NSLog(@"\n-=-=-=-=-=-=-=验证码：%@", codeString);
    };
    [self.view addSubview:_codeView];
}

#pragma mark - 限制输入的只能是金钱
#define myDotNumbers       @"0123456789.\n"
#define myNumbers          @"0123456789\n"

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidLoad) name:UITextFieldTextDidChangeNotification object:textField];
    
    NSUInteger nDotLoc = [textField.text rangeOfString:@"."].location;
    if (NSNotFound != nDotLoc && range.location > nDotLoc + 2) {
        NSLog(@"小数点后最多两位");
        return NO;
    }
    
    NSCharacterSet *cs;
    if (NSNotFound == nDotLoc && 0 != range.location) {
        cs = [[NSCharacterSet characterSetWithCharactersInString:myDotNumbers] invertedSet];
    }
    else {
        cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet];
    }
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    BOOL basicTest = [string isEqualToString:filtered];
    if (!basicTest) {
        return NO;
    }
    
    return YES;
}

@end
